//
//  UIBarButtonItem+Extension.m
//  MedLighter
//
//  Created by apple on 14/12/10.
//  Copyright (c) 2014年 wli. All rights reserved.
//

#import "UIBarButtonItem+Extension.h"

#import "UIView+HBExtension.h"
#import "UIControl+BlocksKit.h"
#import "UIColor+Extension.h"

#define GlobalBuleColor [UIColor HB_ColorWithHexString:@"#3eabf9"] //登陆蓝色
@implementation UIBarButtonItem (Extension)

+ (UIBarButtonItem *)leftItemWithTargeBlock:(void (^)(void))backClick image:(UIImage *)image highImage:(UIImage *)highImage
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn bk_addEventHandler:^(id sender) {
        if (backClick) {
            backClick();
        }
    } forControlEvents:UIControlEventTouchUpInside];
    // 设置图片
    [btn setImage:image forState:UIControlStateNormal];
    [btn setImage:highImage forState:UIControlStateHighlighted];
    btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    // 设置尺寸
    btn.size = CGSizeMake(44, 44);
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0, -3, 0, 0)];
    UIView *view = [[UIView alloc] init];
    view.size = CGSizeMake(44, 44);
    [view addSubview:btn];
    return [[UIBarButtonItem alloc] initWithCustomView:view];
}

+ (UIBarButtonItem *)leftItemBtnWithTargetBlock:(void (^)(void))backClick title:(NSString *)title
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.titleLabel.textAlignment = NSTextAlignmentLeft;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor: GlobalBuleColor forState:UIControlStateNormal];
    [btn setTitleColor:GlobalBuleColor forState:UIControlStateDisabled];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    CGSize size = [title sizeWithAttributes:@{NSFontAttributeName : btn.titleLabel.font}];
    btn.frame = CGRectMake(0, 0, size.width, 44);
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 0);
    [btn bk_addEventHandler:^(id sender) {
        if (backClick) {
            backClick();
        }
    } forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

/**
 *  创建一个item
 *
 *  @param target    点击item后调用哪个对象的方法
 *  @param action    点击item后调用target的哪个方法
 *  @param image     图片
 *  @param highImage 高亮的图片
 *
 *  @return 创建完的item
 */
+ (UIBarButtonItem *)rightItemWithTargetBlock:(void (^)(void))backClick image:(UIImage *)image highImage:(UIImage *)highImage
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn bk_addEventHandler:^(id sender) {
        if (backClick) {
            backClick();
        }
    } forControlEvents:UIControlEventTouchUpInside];
    // 设置图片
    [btn setImage:image forState:UIControlStateNormal];
    [btn setImage:highImage forState:UIControlStateHighlighted];
    btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    // 设置尺寸
    btn.size = CGSizeMake(30, 30); //btn.currentImage.size;
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -3)]; // 向右边拉伸
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}



+ (UIBarButtonItem *)rightItemBtnWithTargetBlock:(void (^)(void))backClick title:(NSString *)title
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.titleLabel.textAlignment = NSTextAlignmentRight;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:GlobalBuleColor forState:UIControlStateNormal];
    [btn setTitleColor:GlobalBuleColor forState:UIControlStateDisabled];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    CGSize size = [title sizeWithAttributes:@{NSFontAttributeName : btn.titleLabel.font}];
    btn.frame = CGRectMake(0, 0, size.width, 44);
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -8);
    [btn bk_addEventHandler:^(id sender) {
        if (backClick) {
            backClick();
        }
    } forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}
+ (UIBarButtonItem *)rightItemBtnWithTargetBlock:(void (^)(void))backClick title:(NSString *)title withColor:(UIColor *)color{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.titleLabel.textAlignment = NSTextAlignmentRight;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateDisabled];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    CGSize size = [title sizeWithAttributes:@{NSFontAttributeName : btn.titleLabel.font}];
    btn.frame = CGRectMake(0, 0, size.width, 44);
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -8);
    [btn bk_addEventHandler:^(id sender) {
        if (backClick) {
            backClick();
        }
    } forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}


@end
